﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 字符串必须包含所有项, 示例:
/// <code>
/// [VMustContainsAll(["男", "未婚"])]
/// public string Labels { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustContainsAllAttribute(string[] values, bool ignoreCase = false, string errorMessage = null) : Attribute, IValidateAction
{
    public string[] Values { get; set; } = values;
    public bool IgnoreCase { get; } = ignoreCase;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateStringExtensions.MustContainsAllInternal(ctx, ctx.ModelValue?.ToString(), Values, IgnoreCase, ErrorMessage);
    }
}

/// <summary>
/// 如果不为null, 则字符串必须包含所有项, 示例:
/// <code>
/// [VMustContainsAllIfNotNull(["男", "未婚"])]
/// public string Labels { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustContainsAllIfNotNullAttribute(string[] values, bool ignoreCase = false, string errorMessage = null) : Attribute, IValidateAction
{
    public string[] Values { get; set; } = values;
    public bool IgnoreCase { get; } = ignoreCase;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateStringExtensions.MustContainsAllInternal(ctx, ctx.ModelValue?.ToString(), Values, IgnoreCase, ErrorMessage);
    }
}
